// mfcdll.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
//#include "network.h"
#include "comdef.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
__declspec(dllexport) long __stdcall bstrToarr1(const VARIANT FAR& vbstr,double * p,long n1)
{
  switch  (vbstr.vt)
  {
  case VT_BSTR:
  	  memcpy(p,vbstr.bstrVal,n1*4);
	  return(n1);
  case VT_UI1 | VT_ARRAY:
	memcpy(p,vbstr.parray->pvData,n1*4);
	return(n1);
  default:
	return(-1);
  }
}
__declspec(dllexport) void __stdcall mymemcpy(char * des,char * src,long n1)
{
	  memcpy(des,src,n1);
}
__declspec(dllexport) long __stdcall bstrToarr(const VARIANT FAR& vbstr,VARIANT & varr)
{
 _variant_t vx;
 if (varr.vt==(VT_VARIANT | VT_BYREF))
 {
      _variant_t vx0=_variant_t(varr);
 	  vx=_variant_t(vx0.pvarVal);
 }
 else{
	if (varr.vt==(VT_R4 | VT_ARRAY))
	{
	   vx=_variant_t(varr);
	}
	else
	{
		return(-1);
	}
 }
	  if (vx.vt==(VT_R4 | VT_ARRAY))
	  {
		  long l=vx.parray->rgsabound->lLbound;
		  long n1=vx.parray->rgsabound->cElements;
		  double *p=(double *)vx.parray->pvData;
		  if (vbstr.vt==VT_BSTR)
		  {
			  memcpy(p,vbstr.bstrVal,n1*4);
			  return(n1);
		  }
	  }
   return(-1);
}
__declspec(dllexport) long  arrTobstr(VARIANT FAR& b,VARIANT & varr,long n)
{
 _variant_t vx;
 if (varr.vt==(VT_VARIANT | VT_BYREF))
 {
      _variant_t vx0=_variant_t(varr);
 	  vx=_variant_t(vx0.pvarVal);
 }
 else{
	if (varr.vt==(VT_R4 | VT_ARRAY))
	{
	   vx=_variant_t(varr);
	}
	else
	{
		return(-1);
	}
 }
	  if (vx.vt==(VT_R4 | VT_ARRAY))
	  {
		  long l=vx.parray->rgsabound->lLbound;
		  long n1=vx.parray->rgsabound->cElements;
		  double *p=(double *)vx.parray->pvData;
		  if (b.vt==VT_UI1 | VT_ARRAY){
			  memcpy(b.parray->pvData,p,n*4);
			  return(n1);
		  }
	  }
   return(-1);
}
__declspec(dllexport) BOOL __stdcall smooth3(VARIANT & x,VARIANT & y,long n,long bAN,double * a,double *b ,long &bcount)
{
	  double *px;
	  double *py;
	  if (x.vt==(VT_R4 | VT_ARRAY))
	  {
		  px=(double *)x.parray->pvData;
	  }
	  else
	  {
		  return(FALSE);
	  }
	  if (y.vt==(VT_R4 | VT_ARRAY))
	  {
		  py=(double *)y.parray->pvData;
	  }
	  else
	  {
		  return(FALSE);
	  }

	  if(n < bAN * 2 + 1){
		  return(FALSE);
	  }
	long i;
	bcount = 0;
	long j;
	for(i = bAN;i< n - bAN;i++)
	{
	   a[bcount] = px[i];
	   for( j = i - bAN ;j< i + bAN+1;j++)
	   {
		  b[bcount] = b[bcount] + py[j];
	   }
	   b[bcount] = b[bcount] / (2 * bAN + 1);
	   bcount = bcount + 1;
	}
	return(TRUE);
}
__declspec(dllexport) void __stdcall koujixian(double *y ,long n)
{
	if(n<1) return;
	long i;
	double  y0;
	y0=*y;
	for(i = 1;i< n;i++)
	{
		if(y0>y[i]) y0=y[i];
	}
	for(i =0;i< n;i++)
	{
		y[i]=y[i]-y0+.0000001;
	}
}

